<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="me.yobol.greentable.commons.path.*"%>
<%@ page import="me.yobol.greentable.util.ckfinder.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>Create Ingredient</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
	<%
		String msg = (String)request.getParameter("msg");
		if (msg != null) {

			//当msg不为空时，说明用户以forward转发方式跳转过来
			//..还是代表的请求重定向发起者——CreateDishesServlet.java文件
			//所以以../ckeditor/ckeditor.js方式便获取不到ckeditor.js文件
			if (msg.equals("IngredientNameDuplication")) {
	%>
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/js/jquery-3.1.1.min.js"></script>
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/ckfinder/ckfinder.js"></script>
	<script>
					alert("已经发布过此食材");
				</script>
	<%
		}
		} else {
			//查看Chrome Console可以发现症结所在
	%>
	<script type="text/javascript" src="../../js/jquery-3.1.1.min.js"></script>
	<script type="text/javascript" src="../../ckfinder/ckfinder.js"></script>
	<%
		}
	%>

	<form action="<%=SPath.sCreateIngredient(request)%>" method="post">
		<input type="text" name="igName" placeholder="食材名" /> 
		<input type="text" name="igDesc" placeholder="食材描述" />
		<img id="igImgContainer" width="150px" height="150px" src="<%=RPath.getImgUrl(request, "unprovided.jpg") %>"/><input id="igImg" type="hidden" name="igImg" value=""/>
		<input type="button" value="上传图片" onClick="uploadImg('<%=CKFinder.chooseUserFolder(request, (Integer) request.getSession().getAttribute("LoginStatus"), (String) request
					.getSession().getAttribute("uName"))%>');">
		<input type="text" name="igPrice" placeholder="食材单价" /> <input type="text" name="igFlag" placeholder="食材标签" />
		<input type="submit" value="发布菜品">
		<script>
			var api;
			var igImgContainer = $("#igImgContainer");
			var igImg = $("#igImg");

			var mydata = function() {
				var selectFunctionData;
			};

			var data = new mydata();
			data.selectFunctionData = "lblInfo";

			function uploadImg(root) {
				var finder = new CKFinder();
				finder.callback = function(api) {//ckfinder创建时函数
					var menuItem = {
						label : "选择",
						command : "selecFile" //其他地方调用命令标识
					};
					//添加选择按钮
					api.addFileContextMenuOption(menuItem,
							function(api, folder) {
								CKFinder.tools.callFunction(ref, data);
							});
				};
				api = finder.popup();

				var ref = CKFinder.tools.addFunction(function(data) {//定义选择按钮回调函数  
					var myapi = this.myapi;
					var files = myapi.getSelectedFiles();//获取选择文件  
					$(files).each(function() {
						//var str = "<a href='" + this.getUrl() + "'>" + this.name + "</a></br>"  
						//$("#" + data["selectFunctionData"]).append($(str));
						var path = root + "images/" + this.name;
						igImgContainer.attr("src",path);
						igImg.val(path);
					});
					myapi.closePopup();//关闭ckfinder
				}, {
					myapi : api
				});

			}
		</script>
	</form>
</body>
</html>
